import * as vscode from 'vscode';
import { SnippetManager } from '../utils/snippetManager';
import { SnippetProvider } from '../providers/snippetProvider';

export class ImportSnippetsCommand {
    constructor(
        private snippetManager: SnippetManager,
        private snippetProvider: SnippetProvider
    ) {}

    get commandId(): string {
        return 'snippetManager.importSnippets';
    }

    async execute(): Promise<void> {
        try {
            // 选择导入文件
            const uri = await vscode.window.showOpenDialog({
                canSelectFiles: true,
                canSelectFolders: false,
                canSelectMany: false,
                filters: {
                    'JSON Files': ['json'],
                    'All Files': ['*']
                }
            });

            if (!uri || uri.length === 0) return;

            // 确认导入
            const confirm = await vscode.window.showWarningMessage(
                'Importing snippets will add them to your existing collection. Continue?',
                'Import',
                'Cancel'
            );

            if (confirm !== 'Import') return;

            // 导入代码片段
            await this.snippetManager.importSnippets(uri[0].fsPath);
            this.snippetProvider.refresh();
            vscode.window.showInformationMessage('Snippets imported successfully!');

        } catch (error) {
            vscode.window.showErrorMessage(`Failed to import snippets: ${error}`);
        }
    }
}
